package com.nbjtjc.safe.api.cost;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.cost.CostBudgetItemMapper;
import com.nbjtjc.safe.model.cost.CostBudgetItem;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.service.cost.CostBudgetItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin("*")
@RequestMapping("/costBudgetItem")
public class CostBudgetItemController {
	@Autowired
	private CostBudgetItemService costBudgetItemService;
	@Autowired
	CostBudgetItemMapper costBudgetItemMapper;

	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新安全投入预算计划项目")
	    public Result add(@RequestBody CostBudgetItem costBudgetItem){
	        Result result=new Result();
	        try {
	            result=costBudgetItemService.add(costBudgetItem);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除安全投入预算计划项目")
	    public Result delete(@RequestBody CostBudgetItem costBudgetItem) {
	        Result result=new Result();
	        try {
	  	      if(ObjectUtil.isNull(costBudgetItem)){
		    	  result.setFalse("传入内容不能为空");
			      return result;
		      }else if(ObjectUtil.isNull(costBudgetItem.getItemTempId())||costBudgetItem.getItemTempId()!=-1){
		    	  result.setFalse("自建项目才能删除");
			      return result;
		      }
	            result=costBudgetItemService.delete(costBudgetItem);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody CostBudgetItem costBudgetItem) {
	        Result result = new Result();

	        try {
	            result = costBudgetItemService.find(costBudgetItem);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/findByYear", method = {RequestMethod.POST})
	  public Result findByYear(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	            result = costBudgetItemService.findByYear(costBudgetPlan);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/show", method = {RequestMethod.POST})
	  public Result show(@RequestBody CostBudgetItem costBudgetItem) {
	        Result result = new Result();

	        try {
	            result = costBudgetItemService.show(costBudgetItem.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
}
